package cn.kgc.config;

import cn.kgc.constant.OrderConstant;
import org.springframework.amqp.core.Binding;
import org.springframework.amqp.core.Exchange;
import org.springframework.amqp.core.Queue;
import org.springframework.amqp.core.TopicExchange;
import org.springframework.amqp.support.converter.Jackson2JsonMessageConverter;
import org.springframework.amqp.support.converter.MessageConverter;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import java.util.HashMap;
import java.util.Map;

import java.util.HashMap;
import java.util.Map;


/**
 * <p>添加描述信息</p>
 * @author 胡东波
 * @since 2023年01月04日 12:08
 *
 * 因为有消息队列的监听
 */
@Configuration
public class RabbitMQConfig {

    @Bean
    public Exchange orderExchange(){
        return new TopicExchange("order.exchange",true,false);
    }

    @Bean
    public Queue orderDelayQueue(){
        Map<String,Object>arguments=new HashMap<>();
        //超时时间
        arguments.put("x-message-ttl",6000);
        //死信交换机
        arguments.put("x-dead-letter-exchange","order.exchange");

        //死信routingKey
        arguments.put("x-dead-letter-routing-key","order.release.order");

        Queue queue=new Queue("order.delay.queue",true,true,false,arguments);
        return queue;
    }

    @Bean
    public Queue orderReleaseQueue(){
        return new Queue(OrderConstant.ORDER_RELEASE_QUEUE,true,true,false);
    }

    //绑定
    @Bean
    public Binding orderDelayQueueBinding(){
        //如果向交换机发送一个信息，如果routingKey是：order.create.order
        //此信息就会进入到order.delay.queue队列中
        return new Binding("order.delay.queue",
                Binding.DestinationType.QUEUE,OrderConstant.ORDER_EXCHANGE,OrderConstant.CREATE_ORDER_ROUTING_KEY,null);

    }

    @Bean
    public Binding orderReleaseQueueBinding(){
        return new Binding("order.release.queue",
                Binding.DestinationType.QUEUE, OrderConstant.ORDER_EXCHANGE,OrderConstant.RELEASE_ORDER_ROUTING_KEY,null);
    }


    //配置消息转换器
    @Bean
    public MessageConverter messageConverter(){
        return new Jackson2JsonMessageConverter();
    }
}